/*
** my_put_in_list.c for my_put_in_list.c in /home/chassa_m/rendu/PSU_2013_my_ls/MA_LISTE_CHAINEE_ELLE_EST_TROP_SWAG
** 
** Made by chassa_m
** Login   <chassa_m@epitech.net>
** 
** Started on  Thu Nov 28 18:37:51 2013 chassa_m
** Last update Fri Nov 29 11:19:14 2013 chassa_m
*/

#include "list.h"
#include <stdlib.h>
#include <string.h>

int		my_put_in_list(t_list **list, char *data, char *value)
{
  t_list	*element;

  element = malloc(sizeof(*element));
  if (element == NULL)
    return (1);
  element->data = strdup(data);
  element->value = strdup(value);
  element->next = *list;
  *list = element;
  return (0);
}

void            my_show_list(t_list *list)
{
  t_list        *tmp;

  tmp = list;
  while (tmp != NULL)
    {
      my_putstr(tmp->value);
      my_putchar('\n');
      tmp = tmp->next;
    }
}

void            my_rev_list(t_list **begin)
{
  t_list        *tmp;
  t_list        *next;
  t_list        *prev;

  prev = NULL;
  tmp = *begin;
  while ((*begin) != NULL)
    {
      next = (*begin)->next;
      (*begin)->next = prev;
      prev = tmp;
      tmp = next;
      *begin = next;
    }
  *begin = prev;
}

void		my_sort_list(t_list **begin, int (*cmp)())
{
  t_list	*tmp;
  t_list	*ref;
  int		swap;

  swap = 1;
  while (swap != 0)
    {
      swap = 0;
      tmp = *begin;
      while (tmp->next != 0)
        {
          if (cmp(tmp->next->data, (*begin)->data) == 0)
            {
              ref = tmp->next;
              tmp->next = ref->next;
              ref->next = *begin;
              *begin = ref;
              swap = 1;
            }
          tmp = tmp->next;
        }
    }
}
